/* Rectangle.h
Copyright (c) 2008 Michael Zahniser
Please note the license terms (MIT license) at the end of this document.

Header file for the Rectangle class, an SDL_Rect with functions to
construct it and modify it.
*/

#ifndef __MZ_RECTANGLE_H__
#define __MZ_RECTANGLE_H__

#include "SDL.h"



class Rectangle : public SDL_Rect {
public:
	Rectangle();
	Rectangle(int x, int y, int width, int height);
	// This also serves as a copy constructor, since the Rectangle can be
	// safely interpreted as an SDL_Rect.
	Rectangle(const SDL_Rect &rect);

	Rectangle &operator=(const SDL_Rect &rect);
	// Check whether two rectangles are equal.
	bool operator==(const SDL_Rect &rect) const;
	bool operator!=(const SDL_Rect &rect) const;
	// Check whether this rectange is empty.
	operator bool() const { return !!*this; }
	bool operator!() const { return (w <= 0) | (h <= 0); }
	// Set the dimensions of this rectangle.
	void Set(int x = 0, int y = 0, int width = 0, int height = 0);
	// Move this rectangle, without resizing it.
	void Move(int dx, int dy);
	// Move this rectangle (without resizing it) to fit in (0, 0, width, height).
	void KeepInside(int width, int height);

	// Check if this rectangle contains the given point.
	bool Contains(int x, int y) const;

	// Get the rectangle start and end coordinates.
	int Left() const { return x; }
	int Top() const { return y; }
	int Right() const { return x + w; }
	int Bottom() const { return y + h; }
	int Width() const { return w; }
	int Height() const { return h; }

	// Rectangle intersection: the result is the rectangle contained in
	// both the input arguments.
	Rectangle operator&(const SDL_Rect &rect) const;
	friend Rectangle operator&(const SDL_Rect &a, const Rectangle &b) { return b & a; }
	Rectangle &operator&=(const SDL_Rect &rect);
	// Rectangle "union": the resut is the rectangle containing both of
	// the input rects. This is not, strictly speaking, the set union of
	// the two areas.
	Rectangle operator|(const SDL_Rect &rect) const;
	friend Rectangle operator|(const SDL_Rect &a, const Rectangle &b) { return b | a; }
	Rectangle &operator|=(const SDL_Rect &rect);
};



#endif



/* Copyright (c) 2008 Michael Zahniser

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE. */
